import React from 'react'
// ===================================================================== Temp
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import TableControls from '#cpt/temp/TableControls'
import Controls from '#cpt/temp/Controls'
import Delete from '#cpt/temp/Delete'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page 					= $Lazy.load(()=>import('#tp/page/page-container'))
const $cache 				= $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 提交表单列表
		formData : [
			{ label: '分组名称',		name: 'merge_name', 		full:true, required:true, },
			{ label: '处理方法',		name: 'handle_type', 		half:true, required:true, type:'select', idStr:'code' },
			{ label: '排序',			name: 'sort', 				half:true, required:true, type:'number' },
			{ label: '报告单模板',	name: 'report_tpl_id', 		half:true, required:true, type:'select' },
			{ label: '描述',			name: 'description', 		full:true, type:'textarea' },
		]
	}
	
	model = {}
	
	componentDidMount(){
		this.fetch()
	}
	
	// 获取弹窗下拉数据
	getModalSelect = async () => {
		const { formData } = this.state
		$fn.setModalRows(formData, [ 'handle_type', 'report_tpl_id' ], ()=>{
			$cache.then( async f => {
				// 处理方法
				$fn.setRowc(formData, 'handle_type',  async () =>  f.getDicCache({ code: 63050}))
				// 报告单模板
				$fn.setRow(formData, 'report_tpl_id', await f.getCache({ api: 'report_template' } ) )
				//
				this.setState({ formData })
			})
		})
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'merge-group/index')
	
	// 表单
	cols = [
		{ title: '分组编号', 	dataIndex: 'merge_no', 			width:150 },
		{ title: '分组名称', 	dataIndex: 'merge_name', 		width:200 },
		{ title: '分组描述', 	dataIndex: 'description', 		width:200 },
		{ title: '处理方法', 	dataIndex: 'handle_type_name', 	width:150 },
		TableControls.controls.call(this)
	]
	// 表格操作按钮
	table_controls = (text,{ uuid }) => {
		return [
			TableControls.edit.call(this, 'merge-group/info', { uuid }, { select:this.getModalSelect}),				// 编辑
			this.onDel([ uuid ]),															// 删除
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this,{click:this.getModalSelect}),			// 添加
			this.onDel(),						// 删除
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'merge-group/del', { uuid, param:{enabled:-1},  isKeys: !uuid, onComplete:()=> $fn.dataRemove('merge-group/select') })
	
	render(){
		return (
			<>
				<Page title='合并规则' controls={this.controls()} con>
					<div className='h10'></div>
					{/* 表格 */}
					{ Table.call(this) }
				</Page>
				{/* 添加、编辑 */}
				{ AddEditModal.call(this,{ api: 'merge-group/add', editApi:'merge-group/edit', onComplete:()=> $fn.dataRemove('merge-group/select') }) }
			</>
		)
	}
}

export default Index